package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * another complex java bean edited with propertyhaverheditor2
 * 
 * @author sg
 * 
 */
public class BeanEditorTest3 implements Test {

	@Override
	public void test(Panel parent) {
		new EditorFramework1().start(); // do not forget of initialize the gwteditors framework !

		ShapeBean sb = GWT.create(ShapeBean.class);

		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		parent.add(vp); // make sure the editor parent (vp) is attached to the dom before using the editor

		PropertyHaverEditor2<ShapeBean> ed = new PropertyHaverEditor2<ShapeBean>();
		vp.add(ed.getWidget());

		ed.load(sb);

		ed.addValueChangeListener(new ValueChangeListener<ShapeBean>() {
			@Override
			public void notifyValueChange(EditorValueChangeEvent<ShapeBean> evt) {
				Window.alert("property value change detected: " + evt.getSrc().flush());
			}
		});
	}

	// The bean we want to edit

	public static abstract class ShapeBean implements EditableBean {

		public static ShapeBean create() {
			ShapeBean bean = GWT.create(ShapeBean.class);
			bean.setStrokeDashArray(
				new SelectionList<String>(
						new String[] { "", ".", "-", "- ." }, new String[] { "" }));
			bean.setFillColor(new Color("ffffff"));
			bean.setStrokeColor(new Color("000000"));
			return bean;
		}

		Color fillColor, strokeColor;
		String fillImage, fillGradient, transform, clipRect, path;
		SelectionList<String> strokeDashArray;
		Integer strokeWidth;
		Double opacity = 1.0;

		public Color getFillColor() {
			return fillColor;
		}

		public void setFillColor(Color fillColor) {
			this.fillColor = fillColor;
		}

		public Color getStrokeColor() {
			return strokeColor;
		}

		public void setStrokeColor(Color strokeColor) {
			this.strokeColor = strokeColor;
		}

		public String getFillImage() {
			return fillImage;
		}

		public void setFillImage(String fillImage) {
			this.fillImage = fillImage;
		}

		public String getFillGradient() {
			return fillGradient;
		}

		public void setFillGradient(String fillGradient) {
			this.fillGradient = fillGradient;
		}

		public SelectionList<String> getStrokeDashArray() {
			return strokeDashArray;
		}

		public void setStrokeDashArray(SelectionList<String> strokeDashArray) {
			this.strokeDashArray = strokeDashArray;
		}

		public void setStrokeDashArraySelection(String val) {
			this.strokeDashArray.getSelection().clear();
			this.strokeDashArray.getSelection().add(val);
		}

		public Integer getStrokeWidth() {
			return strokeWidth;
		}

		public void setStrokeWidth(Integer strokeWidth) {
			this.strokeWidth = strokeWidth;
		}

		public String getTransform() {
			return transform;
		}

		public void setTransform(String transform) {
			this.transform = transform;
		}

		public String getClipRect() {
			return clipRect;
		}

		public void setClipRect(String clipRect) {
			this.clipRect = clipRect;
		}

		public String getPath() {
			return path;
		}

		public void setPath(String path) {
			this.path = path;
		}

		public Double getOpacity() {
			return opacity;
		}

		public void setOpacity(Double opacity) {
			this.opacity = opacity;
		}

	}

	// test stuff
	@Override
	public String getName() {
		return "bean 3";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.BeanEditorTest3());
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "show how to use PropertyHaverEditor2 for editing anidated beans";
	}
}
